package com.zxk.service.impl;

import com.sun.jna.platform.win32.WinDef;
import com.zxk.constant.JNAConstants;
import com.zxk.constant.ParamConstants;
import com.zxk.constant.PicConstants;
import com.zxk.enums.Coord;
import com.zxk.service.AbstractMyTask;
import com.zxk.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * 召唤灵乐园
 *
 * @author: 舟小亢
 * @date: 2023-09-18 21:22
 */
public class CatEdenTaskImpl extends AbstractMyTask {

    private static Logger LOG = LoggerFactory.getLogger(CatEdenTaskImpl.class);

    public CatEdenTaskImpl(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) {
        super(hwnd_0, hwnd_1, hwnd_2);
    }

    @Override
    public boolean execute() throws Exception {
        LOG.info("【召唤灵乐园任务】任务开始！");

        if (TimeUtil.isAfter(21, 40)) {
            LOG.error("【召唤灵乐园任务】活动时间已过！");
            return false;
        }

        // 切换组合1
        if (!changeAssistantTeam(Coord.ASSISTANT_FRIEND_1)) {
            LOG.info("【召唤灵乐园任务】切换助战组合[1]失败！");
        }
        LOG.info("【召唤灵乐园任务】切换助战组合[1]成功！");

        // 任务21:00开始,时间未到则一直等待
        if (TimeUtil.isBefore(21, 0)) {
            // 算下需要睡眠的时间，活动开始后1分钟
            Calendar targetCalendar = Calendar.getInstance();
            targetCalendar.setTime(new Date());
            targetCalendar.set(Calendar.HOUR_OF_DAY, 21);
            targetCalendar.set(Calendar.MINUTE, 0);
            targetCalendar.set(Calendar.SECOND, 10);
            targetCalendar.set(Calendar.MILLISECOND, 0);
            long targetMillis = targetCalendar.getTimeInMillis();
            LocalDateTime now = LocalDateTime.now();
            long sub = targetMillis - System.currentTimeMillis();
            LOG.info("【召唤灵乐园任务】需要休眠相当于[{}]小时，相当于[{}]分钟，相当于[{}]秒！"
                    , (double) sub / (1000 * 60 * 60)
                    , (double) sub / (1000 * 60)
                    , sub / 1000);
            ThreadUtil.sleep(sub);
            // 休眠结束再次重置任务
            goHome();
        }

        // 点击活动参加
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_活动)) {
            return false;
        }
        // 找到日常活动并点击
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_日常活动)) {
            return false;
        }

        // 找到NPC开始任务
        for (int i = 0; i < ParamConstants.UNIFY_TIMES; i++) {
            pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.宠物乐园任务_任务图标, 2);
            if (pic.isFinded()) {
                break;
            }
            // 如果没找到下拉滑动屏幕寻找图标
            MouseUtil.dragMove(hwnd_1, Coord.ACTIVITY_WHELL.getX(), Coord.ACTIVITY_WHELL.getY(), 0, -100);

            // 一直没找到
            if (i == ParamConstants.UNIFY_TIMES - 1) {
                LOG.error("【召唤灵乐园任务】任务失败！");
                return false;
            }
        }
        MouseUtil.lClickRandom(hwnd_1,
                pic.getPicX() + Coord.ATTEND_TASK_DXY.getX(),
                pic.getPicY() + Coord.ATTEND_TASK_DXY.getY());


        // 接收任务
        pic = FuzzyPicUtil.findPicM(hwnd_0, new String[]{PicConstants.宠物乐园任务_副本, PicConstants.宠物乐园任务_接受});
        if (!pic.isFinded()) {
            LOG.error("【召唤灵乐园任务】领取任务失败！");
            return false;
        }
        // 第一次进入需要接受任务,第二次及以后不用选择
        if (pic.getFindResult()[0] == 1) {
            LOG.error("【召唤灵乐园任务】第一次进入任务，选择PVE！");
            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.宠物乐园任务_副本)) {
                return false;
            }
            // 选择宠物乐园副本
            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.宠物乐园任务_确定)) {
                return false;
            }
        } else {
            LOG.error("【召唤灵乐园任务】已选择过任务，继续参加任务！");
            // 继续参加活动
            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.宠物乐园任务_接受)) {
                return false;
            }
        }


        // 找到任务小时钟，开始战斗
        if (FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.宠物乐园任务_任务小时钟, 5)) {
            ThreadUtil.sleep1();
            // 确认战斗
            FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.宠物乐园任务_挑战, 10);
        } else {
            LOG.error("【召唤灵乐园任务】进入战斗失败！");
            return false;
        }

        return true;
    }

    @Override
    public boolean checkTask() throws Exception {
        int count = 0;

        while (true) {
            LOG.info("【召唤灵乐园任务】开始检查任务状态,第[{}]轮！", ++count);

            // 检查战斗状态
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_战斗中, 3).isFinded()) {
                LOG.info("【召唤灵乐园任务】战斗中，休眠10秒！");
                // 战斗中，等待30秒
                ThreadUtil.sleep(10000);
            }

            // 未战斗，点击任务列表，继续战斗
            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.宠物乐园任务_任务小时钟, 5)) {
                LOG.info("【召唤灵乐园任务】继续开始战斗！");
                // 确认战斗
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.宠物乐园任务_挑战, 10);
                LOG.info("【召唤灵乐园任务】确认战斗！");
            }

            // 检查任务时间
            if (TimeUtil.isAfter(21, 40)) {
                LOG.warn("【召唤灵乐园任务】任务时间已过，任务完成！");
                break;
            } else {
                LOG.info("【召唤灵乐园任务】任务还在进行中！");
            }
        }
        return true;
    }
}
